#include <QtGui>
#include "giftpixmap.h"
#include "mainwindow.h"
#include "transitionbutton.h"

AnimationView::AnimationView( QWidget *parent ) : QGraphicsView( parent )
{
	scene = new QGraphicsScene(-500, -500, 1000, 1000) ;
	setObjectName("MyGraphicsView");
	flowerCount = 0 ;
	uid = 0 ;
	animationViewInit();
}

AnimationView::AnimationView( QGraphicsScene * scene, QWidget * parent )
: QGraphicsView( scene, parent )
{
	Q_ASSERT(scene) ;
	setObjectName("MyGraphicsView");
	this->scene = scene ;
	scene->setObjectName("MyScene");

	animationViewInit();
}

void AnimationView::animationViewInit( )
{
	setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint );

	setAttribute(Qt::WA_TranslucentBackground) ;
	setAttribute(Qt::WA_X11DoNotAcceptFocus);

	setStyleSheet("background:transparent;border:0px;");

	setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

	setScene(scene);

	QPixmap gift(":/Animation/flower.png");

	for (int i = 0; i < bitmapNum; ++i)
	{
		Pixmap *item = new Pixmap(gift);
		item->setObjectName(QString("PixmapItem_")+i);
		item->setOffset(-gift.width()/2, -gift.height()/2);
		item->setZValue(i);
		item->setOpacity(0.0);
		items << item;
		addItem(item);
	}

	// Buttons
	/*giftBtn = new Button(gift) ;
	addItem(giftBtn);
	giftBtn->scale(0.75, 0.75);
	giftBtn->setPos(500, 500);
	giftBtn->setZValue(65);*/
	giftBtn = 0 ;

	generateState();

	server.listen("FlowerComponent");
	connect(&server, SIGNAL(newConnection()), this, SLOT(on_newConnection()));
}

void AnimationView::keyPressEvent(QKeyEvent *event)
{
	if(Qt::Key_Alt == event->key()) isAlt = true ;
}

void AnimationView::keyReleaseEvent(QKeyEvent * event)
{
	if(Qt::Key_Alt == event->key()) isAlt = false ;
}

void AnimationView::closeEvent(QCloseEvent * event)
{
	if(!isAlt)
		QGraphicsView::closeEvent(event);
	else
		event->ignore();
}

void AnimationView::addItem( QGraphicsItem *item )
{
	scene->addItem(item);
}

void AnimationView::on_newConnection()
{
	suc_socket = server.nextPendingConnection() ;
	connect(suc_socket, SIGNAL(readyRead()), this, SLOT(on_readyRead()));
}

void AnimationView::on_readyRead()
{
	QByteArray ary = suc_socket->readAll() ;
	QList<QByteArray> list = ary.split(',');

	quint32 id = list[0].toUInt();
	quint32 count = list[1].toUInt();

	if(id)
	{
		if(0 == uid || uid != id)
		{
			uid = id ;
			flowerCount = count ;
			return ;
		}

		if(count > flowerCount && (count - flowerCount) >= 50)
		{
			flowerCount = count - count % 50;
			showFullScreen();
			states.start();
		}
	}	
}

QState* AnimationView::createChildState( QState* state, int duration )
{
	QState *childState = new QState(state) ;
	QFinalState *finalState = new QFinalState(state) ;
	state->setInitialState(childState) ;

	QTimer *timer = new QTimer(state) ;
	timer->setInterval(duration);
	timer->setSingleShot(true);

	QObject::connect( childState, SIGNAL(entered()), timer, SLOT(start()) );

	childState->addTransition(timer, SIGNAL(timeout()), finalState);

	return childState ;
}

void AnimationView::generateState()
{
	items = getItems();
	giftBtn = getGiftBtn();

	QState *randomState = new QState(&states);
	QState *centeredState = new QState(&states);
	QState *loveLetterState = new QState(&states);

	QState *hideState = new QState(loveLetterState);
	QState *loveLetterChild = new QState(loveLetterState);
	loveLetterState->setInitialState(loveLetterChild);
	states.setInitialState(centeredState);

	QState *randomChild = createChildState( randomState, 2000 );
	QState *centeredChild = createChildState( centeredState, 0 );
	int horizontalUnit = width() / 4 ;

	QParallelAnimationGroup *group = new QParallelAnimationGroup;
	QParallelAnimationGroup *opacityGroup = new QParallelAnimationGroup ;

	for (int i = 0; i < items.count(); ++i)
	{
		Pixmap *item = items.at(i);

		randomChild->assignProperty(item, "pos", QPointF(-250 + qrand() % 500, -250 + qrand() % 500));
		centeredChild->assignProperty(item, "pos", QPointF());
		centeredChild->assignProperty(item, "opacity", 1.0);
		hideState->assignProperty(item, "opacity", 0.0);
		LoveAnimation::generateLoveState(loveLetterChild, item, horizontalUnit, i);

		QPropertyAnimation *anim = new QPropertyAnimation(items[i], "pos");
		anim->setDuration(750 + i * 25);
		anim->setEasingCurve(QEasingCurve::InOutBack);
		group->addAnimation(anim);

		anim = new QPropertyAnimation(items[i], "opacity");
		anim->setDuration(2500);
		anim->setEasingCurve(QEasingCurve::InOutBack);
		opacityGroup->addAnimation(anim);
	}

	QObject::connect( giftBtn, SIGNAL(pressed()), &states, SLOT(start())) ;

	QAbstractTransition *trans = centeredState->addTransition( centeredChild, SIGNAL(exited()), randomState );
	trans->addAnimation(group);

	trans = randomState->addTransition( randomChild, SIGNAL(exited()), loveLetterState );
	trans->addAnimation(group);

	QTimer *timer = new QTimer(loveLetterState) ;
	timer->setInterval(3000);
	timer->setSingleShot(true);
	QObject::connect(loveLetterChild, SIGNAL(entered()), timer, SLOT(start()));
	trans = loveLetterChild->addTransition( timer, SIGNAL(timeout()), hideState);
	trans->addAnimation(opacityGroup);

	trans = centeredState->addTransition( centeredState, SIGNAL(finished()), randomState );
	trans->addAnimation(group);

	trans = randomState->addTransition( randomState, SIGNAL(finished()), loveLetterState );
	trans->addAnimation(group);

	connect(hideState, SIGNAL(entered()), &states, SLOT(stop()));
}